﻿BANGGIA = function (tu,den,gia){ 
    this.tu = tu;
    this.den = den;
    this.gia = gia;
}
BANGGIA.columns = new Array("Tu", "Den", "DonGia", "Ngay");
BANGGIA.fieldText = new Array("Tu", "Den", "DonGia");
BANGGIA.id = -1;
BANGGIA.isInsert = 0;
var numberPage = 1;
BANGGIA.arrays = new Array();
BANGGIA.Init = function () {
    BANGGIA.arrays = new Array();
    var obj;
    obj = new CheckField("Tu", 2, "ErrTu");
    BANGGIA.Add(obj);
    obj = new CheckField("Den", 2, "ErrDen");
    BANGGIA.Add(obj);
    obj = new CheckField("DonGia", 2, "ErrDonGia");
    BANGGIA.Add(obj);
}

BANGGIA.Add = function (obj) {
    if (BANGGIA.arrays.contains(obj) == false) {
        BANGGIA.arrays.push(obj);
    }
}

BANGGIA.Insert = function () {
    var serviceUrl = Constants.service + "insertTblBangGias";    
    if (Constants.OnSubmit(BANGGIA.arrays) == false) {
        return false;
    }
    var data = {
        Tu: $("#Tu").val(),
        Den: $("#Den").val(),
        DonGia: $("#DonGia").val(),
        Ngay: "/Date(1400746875297+0700)/"
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            BANGGIA.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
    return false;
}

BANGGIA.Delete = function (id) {
    var data = {
        id : id
    };
    var json = JSON.stringify({ item: data });
    a = confirm(Constants.messDelete);
    if (a == false) {
        return ;
    }
    var serviceUrl = Constants.service + "deleteTblBangGias";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (data) {
            BANGGIA.GetData();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

BANGGIA.UpdateItem = function () {
    var serviceUrl = Constants.service + "updateTblBangGias";
    var data = { id: $("#id").val(),
        Tu: $("#Tu").val(),
        Den: $("#Den").val(),
        DonGia: $("#DonGia").val()
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            BANGGIA.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

BANGGIA.CreateTable = function (r, nbpage, data) {
    var myTable = BANGGIA.TitleColumns(BANGGIA.columns);
    myTable += BANGGIA.DataColumnsOftr(data, r, nbpage);
    myTable += "</table>";
    $("#div_list").children().remove();
    $("#div_list").append(myTable);
    $("#div_content").show();
}

BANGGIA.GetData = function () {
    var serviceUrl = Constants.service + "getAllTblBangGias";
    var data = { url: window.location.host };
    var json = JSON.stringify({ item: data });
    BANGGIA.id = -1;
    BANGGIA.isInsert = 0;
    // Constants.LoadBangGia();
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var results = msg.getAllTblBangGiasResult;
            var dataTables = results;
            BANGGIA.CreateTable(Constants.rows, numberPage, dataTables);
            var arrs = new Array();
            for (var i = 0; i < results.length; i++) {
                var obj = {
                    Tu: results[i].Tu,
                    Den: results[i].Den,
                    DonGia: results[i].DonGia
                }
                arrs.push(obj);
            }
            if (arrs.length > 0) {
                Constants.Save("banggia", arrs);
            }
            var date = new Date(1400746875297 + 0700);
            console.log("date : " + date.toLocaleString());
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

BANGGIA.TitleColumns = function (_columns) {
    var myColumns = "<table class=\"rounded-corner\">";
    myColumns += "<thead><tr>"; //<th class=\"rounded-company\" scope=\"col\"></th>
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"rounded\" scope=\"col\">" + _columns[i] + "</th>";
    }
    myColumns += "<th class=\"rounded\" scope=\"col\">Edit</th>";
    myColumns += "<th class=\"rounded-q4\" scope=\"col\">Delete</th>";
    myColumns += "</tr></thead>";
    var n = _columns.length + 1;
    myColumns += "<tfoot><tr><td class=\"rounded-foot-left\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"rounded-foot-right\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}


BANGGIA.DataColumnsOftr = function (data, rs, _numberPage) {
    var myDataColumns = "<tbody>";
    var n = data.length;
    var j = (_numberPage - 1) * rs;
    var r = rs;
    for (var i = j; i < r && i < n; i++) {
        var id = data[i].id;
        var dateAsFromServerSide = data[i].Ngay.replace(")/", "");
        var parsedDate = new Date(parseInt(dateAsFromServerSide.substr(6)));        
        myDataColumns += "<tr onclick='return BANGGIA.OnClick(" + id + ")'" + " id=\"" + id + "\">";
        myDataColumns += "<td>" + data[i].Tu + "</td>"
        myDataColumns += "<td>" + data[i].Den + "</td>"
        myDataColumns += "<td>" + data[i].DonGia + "</td>"
        myDataColumns += "<td>" + Constants.formatDate(parsedDate) + "</td>"
        myDataColumns += "<td><img onclick='return BANGGIA.EditItem(" + id + ")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/user_edit.png\"></td>";
        myDataColumns += "<td><a class=\"ask\" ><img onclick='return BANGGIA.Delete(" + id + ")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/trash.png\"></a></td>";
    }
    myDataColumns += "</tbody>";
    return myDataColumns;
}

BANGGIA.OnClick = function (id) {
    BANGGIA.id = id;
    $(".active").removeClass();
    $("#" + id).addClass("active");
    return false;
}

BANGGIA.OnClickEdit = function () {
    if (BANGGIA.id == -1) {
        alert(Constants.selectEdit);
    }
    else
        BANGGIA.EditItem(BANGGIA.id);
}

BANGGIA.OnClickDelete = function () {
    if (BANGGIA.id == -1) {
        alert(Constants.selectDelete);
    }
    else
        BANGGIA.Delete(BANGGIA.id);
}

BANGGIA.EditItem = function (obj_id) {
    var temp_item = "Modules/bg/Edit";
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Sửa" });           
            BANGGIA.GetItem(obj_id);
        }
    });
}

BANGGIA.GetItem = function (id) {
    var serviceUrl = Constants.service + "getItemByIdTblBangGia";
    var data = { id: id };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getItemByIdTblBangGiaResult;
            if (result != null && result != undefined) {
                $("#id").val(result.id);
                $("#Tu").val(result.Tu);
                $("#Den").val(result.Den);
                $("#DonGia").val(result.DonGia);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

BANGGIA.Clear = function () {
    Constants.clearAll(BANGGIA.fieldText);
    Constants.Required(BANGGIA.arrays);
}

BANGGIA.Enter = function () {
    $("#Tu").keypress(function (event) {
        if (event.keyCode == 13) {
            $("#Den").focus();
            event.preventDefault();
        }
    });
    $("#Den").keypress(function (event) {
        if (event.keyCode == 13) {
            $("#DonGia").focus();
            event.preventDefault();
        }
    });

    $("#DonGia").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            if (BANGGIA.isInsert == 1) {
                BANGGIA.Insert();
            }
            else {
                BANGGIA.UpdateItem();
            }
        }
    });

}

BANGGIA.AddNewItem = function () {
    var temp_item = "Modules/bg/AddNew";
    BANGGIA.isInsert = 1;
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Thêm Mới" });
        }
    });
}

$(document).ready(function () {
    BANGGIA.GetData();
    BANGGIA.Init();
    $("#search").keypress(function (event) {
        if (event.keyCode == 13) {
            //fnSearch($("#search").val());
            //createTable(rows, numberPage, dataSearch);
            //event.preventDefault();
        }
    });
});